package com.tiger.ejbql.sample3;

import java.util.*;
import javax.ejb.*;
import javax.naming.*;

public class EJBQLTest3Bean implements javax.ejb.SessionBean {
    private javax.ejb.SessionContext sessionContext = null;
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) {
        this.sessionContext = sessionContext;
    }
    
    public void createTestData() {
        try {
            InitialContext ctx = new InitialContext();
            EJBQLA3Home aHome = (EJBQLA3Home)ctx.lookup("java:comp/env/ejb/EJBQLA3Bean");
            EJBQLB3Home bHome = (EJBQLB3Home)ctx.lookup("java:comp/env/ejb/EJBQLB3Bean");
            
            // テストデータの作成
            EJBQLA3 a1 = aHome.create(new Integer(1), "A1", 2.0);
            EJBQLA3 a2 = aHome.create(new Integer(2), "A2", 3.0);
            EJBQLA3 a3 = aHome.create(new Integer(3), "A1A2", 3.25);
            EJBQLA3 a4 = aHome.create(new Integer(4), "A2A1", -4.15);
            EJBQLA3 a5 = aHome.create(new Integer(5), null, 2.95);
            
            EJBQLB3 b1 = bHome.create(new Integer(1), "B1", 1.0);
            EJBQLB3 b2 = bHome.create(new Integer(2), "B2", 1.15);
            EJBQLB3 b3 = bHome.create(new Integer(3), "B3", 2.0);
            EJBQLB3 b4 = bHome.create(new Integer(4), "B4", -2.0);
            
            a1.getEjbqlb3().add(b1);
            a1.getEjbqlb3().add(b2);
            
            a2.getEjbqlb3().add(b3);
            
            a3.getEjbqlb3().add(b4);
        }
        catch(javax.naming.NamingException e) {
            throw new javax.ejb.EJBException(e);
        }
        catch(javax.ejb.CreateException e) {
            throw new javax.ejb.EJBException(e);
        }
    }
    
    public void test() {
        try {
            InitialContext ctx = new InitialContext();
            EJBQLA3Home aHome = (EJBQLA3Home)ctx.lookup("java:comp/env/ejb/EJBQLA3Bean");
            EJBQLB3Home bHome = (EJBQLB3Home)ctx.lookup("java:comp/env/ejb/EJBQLB3Bean");
            
            // finderメソッド
            System.out.println("BETWEEN句: BETWEEN (2.0, 3.0)");
            Iterator betweenResults = aHome.findTestBetween(2.0, 3.0).iterator();
            while(betweenResults.hasNext()) {
                EJBQLA3 a = (EJBQLA3)betweenResults.next();
                System.out.println("id = " + a.getId() +
                " name = " + a.getName() +
                " value = " + a.getValue());
            }
            System.out.println();
            System.out.println("IN句: IN('A1', 'A2', 'A3')");
            Iterator inResults = aHome.findTestIn().iterator();
            while(inResults.hasNext()) {
                EJBQLA3 a = (EJBQLA3)inResults.next();
                System.out.println("id = " + a.getId() +
                " name = " + a.getName() +
                " value = " + a.getValue());
            }
            System.out.println();
            System.out.println("LIKE句: LIKE '%1'");
            Iterator likeResults = aHome.findTestLike().iterator();
            while(likeResults.hasNext()) {
                EJBQLA3 a = (EJBQLA3)likeResults.next();
                System.out.println("id = " + a.getId() +
                " name = " + a.getName() +
                " value = " + a.getValue());
            }
            System.out.println();
            System.out.println("IS NULL句:");
            Iterator isNullResults = aHome.findTestIsNull().iterator();
            while(isNullResults.hasNext()) {
                EJBQLA3 a = (EJBQLA3)isNullResults.next();
                System.out.println("id = " + a.getId() +
                " name = " + a.getName() +
                " value = " + a.getValue());
            }
            System.out.println();
            System.out.println("IS EMPTY句:");
            Iterator isEmptyResults = aHome.findTestIsEmpty().iterator();
            while(isEmptyResults.hasNext()) {
                EJBQLA3 a = (EJBQLA3)isEmptyResults.next();
                System.out.println("id = " + a.getId() +
                " name = " + a.getName() +
                " value = " + a.getValue());
            }
            aHome.test();
        }
        catch(javax.naming.NamingException e) {
            throw new javax.ejb.EJBException(e);
        }
        catch(javax.ejb.FinderException e) {
            throw new javax.ejb.EJBException(e);
        }
    }
    
    public void ejbCreate() {}
    
    public void ejbActivate() {}
    
    public void ejbPassivate() {}
    
    public void ejbRemove() {}
}
